{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe6d9227-a64f-44ed-9c5f-5e73fb766e43",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "def btnclick(value):\n",
    "    return f\"Hello：{value}\"\n",
    "\n",
    "\n",
    "def changeValue(value):\n",
    "    return value\n",
    "\n",
    "\n",
    "def uploadImg(value):\n",
    "    return np.fliplr(value)\n",
    "\n",
    "\n",
    "def echo(message, history):\n",
    "    if message == \"几楼\":\n",
    "        return \"2楼\"\n",
    "    if message == \"朝向\":\n",
    "        return \"南北\"\n",
    "    if message == \"临街吗\":\n",
    "        return \"不临街\"\n",
    "    return \"我是智能助手,您可以留下联系方式,我将在第一时间给您回复\"\n",
    "\n",
    "\n",
    "with gr.Blocks() as demo:\n",
    "    gr.Markdown(\"官网文档：https://www.gradio.app/docs\")\n",
    "    with gr.Tab(\"基本组件\"):\n",
    "        with gr.Accordion(\"按钮样式\"):\n",
    "            with gr.Row():\n",
    "                with gr.Column(scale=1):\n",
    "                    gr.Button(\"基本按钮\")\n",
    "                    gr.Button(\"禁用按钮\", interactive=False)\n",
    "                    gr.Button(\"小按钮\", size=\"sm\")\n",
    "                with gr.Column(scale=1):\n",
    "                    gr.Button(\"不同颜色按钮\", variant=\"primary\")\n",
    "                    gr.Button(\"不同颜色按钮\", variant=\"stop\")\n",
    "                    gr.Button(\"带图标按钮\", icon=\"https://mis2020-huidu.517.cn/favicon.ico\")\n",
    "        with gr.Accordion(\"按钮点击事件\"):\n",
    "            with gr.Row():\n",
    "                btnClickFromTxt = gr.Textbox(show_label=False, container=False)\n",
    "                btnClickToTxt = gr.Textbox(show_label=False, container=False)\n",
    "                gr.Button(\"点击事件\").click(btnclick, inputs=btnClickFromTxt, outputs=btnClickToTxt)\n",
    "        with gr.Accordion(\"单选按钮\"):\n",
    "            with gr.Row():\n",
    "                radioBtns = gr.Radio([(\"A\", 0), (\"B\", 1), (\"C\", 2)], value=0, show_label=False, container=False)\n",
    "                radioTxt = gr.Textbox(show_label=False, container=False, value=\"0\")\n",
    "                radioBtns.change(changeValue, radioBtns, radioTxt)\n",
    "        with gr.Accordion(\"多选按钮\"):\n",
    "            with gr.Row():\n",
    "                checkboxBtns = gr.CheckboxGroup([(\"A\", 0), (\"B\", 1), (\"C\", 2)], value=0, show_label=False, container=False)\n",
    "                checkboxTxt = gr.Textbox(show_label=False, container=False, value=\"[0]\")\n",
    "                checkboxBtns.change(changeValue, checkboxBtns, checkboxTxt)\n",
    "        with gr.Accordion(\"下拉列表\"):\n",
    "            with gr.Row():\n",
    "                dropdownList = gr.Dropdown([(\"A\", 0), (\"B\", 1), (\"C\", 2)], value=0, show_label=False, container=False)\n",
    "                dropdownTxt = gr.Textbox(show_label=False, container=False, value=\"0\")\n",
    "                dropdownList.change(changeValue, dropdownList, dropdownTxt)\n",
    "    with gr.Tab(\"布局\"):\n",
    "        with gr.Accordion(\"一行排列，需要设置按钮最小宽度才能放下\"):\n",
    "            with gr.Row():\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "                gr.Button(min_width=10)\n",
    "        with gr.Accordion(\"一行排列，自适应宽度\"):\n",
    "            with gr.Row():\n",
    "                gr.Button()\n",
    "                gr.Button()\n",
    "                gr.Button()\n",
    "        with gr.Accordion(\"混合排列\"):\n",
    "            with gr.Row():\n",
    "                with gr.Column(scale=2):\n",
    "                    with gr.Row():\n",
    "                        gr.Button()\n",
    "                    with gr.Row():\n",
    "                        gr.Button()\n",
    "                        gr.Button()\n",
    "                with gr.Column(scale=1):\n",
    "                    gr.Button()\n",
    "    with gr.Tab(\"高级组件\"):\n",
    "        with gr.Accordion(\"图片组件\"):\n",
    "            with gr.Row():\n",
    "                imgFrom = gr.Image()\n",
    "                imgTo = gr.Image()\n",
    "                imgFrom.upload(uploadImg, imgFrom, imgTo)\n",
    "        with gr.Accordion(\"chat组件\"):\n",
    "            gr.ChatInterface(fn=echo, examples=[\"几楼\", \"朝向\", \"临街吗\"], title=\"微聊机器人\", css=\"height:100vh\")\n",
    "\n",
    "demo.launch(server_name=\"*\", server_port=8804, share=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04058969-3ae9-475c-945e-ef9a6181bc67",
   "metadata": {},
   "outputs": [],
   "source": [
    "!curl -sSL https://ngrok-agent.s3.amazonaws.com/ngrok.asc \\\n",
    "\t| sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null \\\n",
    "\t&& echo \"deb https://ngrok-agent.s3.amazonaws.com buster main\" \\\n",
    "\t| sudo tee /etc/apt/sources.list.d/ngrok.list \\\n",
    "\t&& sudo apt update \\\n",
    "\t&& sudo apt install ngrok"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc25310d-b330-4dce-9a12-56db9fdef139",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ngrok config add-authtoken 2OGaG1dlCOclVmhQqUoov4hL0xT_31UoeUfdJc3YH5btC9aLZ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87cc1b61-bb1d-4558-8441-dbba5db48a47",
   "metadata": {},
   "outputs": [],
   "source": [
    "!nohup ngrok http 8804 &"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
